package com.cw.spider.parse;

import cn.wanghaomiao.seimi.def.BaseSeimiCrawler;
import cn.wanghaomiao.seimi.struct.Response;
import com.cw.spider.entity.NewsModel;
import com.cw.spider.service.NewService;
import org.apache.commons.lang3.StringUtils;
import org.seimicrawler.xpath.JXDocument;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 描述:
 *
 * @author withqianqian@163.com
 * @create 2020-08-31 16:47
 */
public class AbstractCrawler extends BaseSeimiCrawler {
    protected String[] url;
    /**
     * @return 设置起始url
     */
    @Override
    public String[] startUrls() {
        return this.url;
    }


    /**
     * 针对startUrl生成首批的response回调这个初始接口
     *
     * @param response --
     */
    @Override
    public void start(Response response) {

    }

    public void setUrl(String[] url) {
        this.url = url;
    }

    public void getArticle(Response response,NewService newService) {
        if (response != null) {
            JXDocument doc = response.document();
            if (doc != null) {
                String titles = (String) doc.selOne("//h1/text()");
                if (StringUtils.isNotEmpty(titles)) {
                    List<Object> content = doc.sel("//div[@class='main']//p/text()");
                    String description = (String) doc.selOne("//div[@class='content']/div[@class='description']/text()");
                    String date = (String) doc.selOne("//div[@class='info']/span[@class='date']/text()");
                    List<String> contents = content.stream().map(o -> o.toString()).collect(Collectors.toList());
                    NewsModel newsModel = new NewsModel.Builder()
                            .title(titles)
                            .content(contents)
                            .publishTime(date)
                            .description(description)
                            .url(response.getUrl())
                            .build();

                    newService.save(newsModel);
                }


            }
        }

    }

    public void getXinHuaArticle(Response response ,NewService newService) {
        JXDocument doc = response.document();
        if (doc != null) {
            String titles = (String) doc.selOne("//title/text()");
            if (StringUtils.isNotEmpty(titles)) {
                List<Object> content = doc.sel("//div[@id='p-detail']//p/text()");
                String date = (String) doc.selOne("//div[@class='h-info']/span[@class='h-time']/text()");
                String source = (String) doc.selOne("//em[@id='source']/text()");
                List<String> contents = content.stream().map(o -> o.toString()).collect(Collectors.toList());
                NewsModel newsModel = new NewsModel.Builder()
                        .title(titles)
                        .content(contents)
                        .publishTime(date)
                        .source(source)
                        .url(response.getUrl())
                        .build();
                newService.save(newsModel);
//                logger.info("news:{}", newsModel);
            }


        }

    }


}
